﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmNVChiTietNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public int thuchien = 0;
        public frmNVChiTietNhanVien()
        {
            InitializeComponent();
        }
        public frmNVChiTietNhanVien(string _manv,string _mane)
        {
            InitializeComponent();
            loadDsChinhsachHT(_manv);
            loadDsCodeDiemThoaiNV(_manv);
            loadThongTinNV(_manv);
            this.Text = "Thông tin nhân viên : " + _manv + " - " + _mane;
            loadThongTinBanGiao(_manv);
        }

        private void loadDsChinhsachHT(string manv)
        {
            gridChinhSach.DataSource = Class.NV_ChinhSachHeThong.HienThiChinhSachofNhanVien(manv);
        }
        private void loadDsCodeDiemThoaiNV(string manv)
        {
            DataTable dt = Class.NV_CodeDienThoai.HienThiCodeNhanVien(manv);
            if (dt.Rows.Count > 0)
            {
                gridCodeDienThoai.DataSource = dt;
                txtNgayCapCode.Text = dt.Rows[0]["NgayCap"].ToString();
                txtNgayHuyCode.Text = dt.Rows[0]["NgayHuy"].ToString();
            }
        }

        private void loadThongTinNV(string manv)
        {
           DataTable dt = Class.NV_NhanVien.HienThiDsNhanVienSearch(manv,"MaNv",1);
           txtMaNV.Text = dt.Rows[0]["MaNv"].ToString();
           txtTenNV.Text = dt.Rows[0]["TenNV"].ToString();
           txtDienThoaiNB.Text = dt.Rows[0]["DienThoaiNB"].ToString();
           txtDienThoaiDD.Text = dt.Rows[0]["DienThoaiDD"].ToString();
           txtMail.Text = dt.Rows[0]["Mail"].ToString();
           txtPhong.Text = dt.Rows[0]["MaPhong"].ToString();
           txtTaiKhoan.Text = dt.Rows[0]["TaiKhoan"].ToString();
           txtMatKhau.Text = dt.Rows[0]["MatKhau"].ToString();
        }
        private void loadThongTinBanGiao(string manv)
        {
            gridBanGiaoThietBi.DataSource = Class.CV_BanGiaoThietBi.HienThiDanhSachBanGiao(manv);
            gridChiThietBanGiao_Click(null, null);
            fnc_CreateIndex(gridChiThietBanGiao, ctbg1);
        }
        
        // ham tao So thu tu
        private void fnc_CreateIndex(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView gridname, DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn columname)
        {
           
            for (int i = 0; i < gridname.RowCount; i++)
            {
                gridname.SetRowCellValue(i, columname.FieldName, i + 1);
            }
            gridname.BestFitColumns();
        }


        private void loadThongTinChiTietMay(string _maMay)
        {
            gridLinhKien.DataSource = Class.MT_CTMayViTinh.HienthidsCTMayInfo(_maMay);
            fnc_CreateIndex(gridDanhSachLinhKien, ct_colum1);
        }


        private void btnCapNhatNhanVien_Click(object sender, EventArgs e)
        {
            Class.NV_NhanVien nv = new Class.NV_NhanVien();
            nv.MaNV = txtMaNV.Text;
            nv.TenNV = txtTenNV.Text;
            nv.TenPhong = txtPhong.Text;
            nv.DienThoaiNB = txtDienThoaiNB.Text;
            nv.DienThoaiDD = txtDienThoaiDD.Text;
            nv.Mail = txtMail.Text;
            nv.TaiKhoan = txtTaiKhoan.Text;
            nv.MatKhau = txtMatKhau.Text;
            try
            {
                if (Class.NV_NhanVien.CapNhatNhanVien(nv))
                {
                    MessageBox.Show("Cập nhật thành công .!!");
                    thuchien = 1;
                   // frmNVQuanLyNhanVien frm = new frmNVQuanLyNhanVien();
                 //  frmNVQuanLyNhanVien.bt
                       //.btnXemNVTheoPhong_Click(null,null);
                    (this.Owner as frmNVQuanLyNhanVien).btnXemNVTheoPhong_Click(null, null);
                    
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại .!!");
                }
            }
            catch
            {
                MessageBox.Show("Có lỗi. Cập nhật thất bại .!!");
            }
        }

        private void checkCreateMail_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCreateMail.Checked)
            {
                string _strsuly;
                _strsuly = txtTenNV.Text.Trim();
                _strsuly = _strsuly.ToLower();
                if (_strsuly.Length > 0)
                {
                    string[] _sulychucai;
                    string KQ = "";
                    _sulychucai = _strsuly.Split(' ');
                    for (int i = 0; i < _sulychucai.Length; i++)
                    {
                        string _chucaidau = _sulychucai[i].Substring(0, 1);
                        _chucaidau = _chucaidau.ToUpper();
                        _sulychucai[i] = _chucaidau + _sulychucai[i].Substring(1, _sulychucai[i].Length - 1);

                        KQ += _sulychucai[i] + " ";
                    }
                    string tk = "";
                    for (int i = 0; i < _sulychucai.Length-1; i++)
                    {
                        string _chucaidau = _sulychucai[i].Substring(0, 1);
                        _chucaidau = _chucaidau.ToLower();
                        tk += _chucaidau;
                    }
                    tk += _sulychucai[_sulychucai.Length - 1].ToLower();
                    tk = bodautiengviet(tk);

                    txtTenNV.Text = KQ.Trim();
                    _strsuly = bodautiengviet(_strsuly);
                    txtMail.Text = _strsuly + "@lbc.com.vn";

                    txtTaiKhoan.Text = tk;
                }
            }
            else
            {
                txtMail.Text = "";
                txtTaiKhoan.Text = "";
            }
        }
        private string bodautiengviet(string chuoi)
        {

            for (int i = 1; i < VietnameseSigns.Length; i++)
            {

                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    chuoi = chuoi.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

            }
            string[] chuoi_mail = chuoi.Split(' ');
            switch (chuoi_mail.Length)
            {
                case 2:
                    chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + "@lbc.com.vn";
                    break;
                case 3:
                    chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + "." + chuoi_mail[2].ToString();
                    break;
                case 4:
                    chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + chuoi_mail[2].ToString() + "." + chuoi_mail[3].ToString();
                    break;
                case 5:
                    chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + chuoi_mail[2].ToString() + chuoi_mail[3].ToString() + "." + chuoi_mail[4].ToString();
                    break;
            }
            return chuoi;
        }
        private static readonly string[] VietnameseSigns = new string[]

         {
        "aAeEoOuUiIdDyY",
        "áàạảãâấầậẩẫăắằặẳẵ",
        "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
        "éèẹẻẽêếềệểễ",
        "ÉÈẸẺẼÊẾỀỆỂỄ",
        "óòọỏõôốồộổỗơớờợởỡ",
        "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
        "úùụủũưứừựửữ",
        "ÚÙỤỦŨƯỨỪỰỬỮ",
        "íìịỉĩ",
        "ÍÌỊỈĨ",
        "đ",
        "Đ",
        "ýỳỵỷỹ",
        "ÝỲỴỶỸ"
         };

        private void checkAnMatKhau_CheckedChanged(object sender, EventArgs e)
        {
            if (checkAnMatKhau.Checked)
            {
                txtMatKhau.Properties.PasswordChar = '\0';
            }
            else
            {
                txtMatKhau.Properties.PasswordChar = '*';
            }
        }

        private void gridChiThietBanGiao_Click(object sender, EventArgs e)
        {
            int SelectedRow = gridChiThietBanGiao.FocusedRowHandle;
            if (SelectedRow >= 0)
            {
                DataRow drow = gridChiThietBanGiao.GetDataRow(SelectedRow);
                string _value = drow["MaMay"].ToString();
                loadThongTinChiTietMay(_value);
            }
        }

        private void checkShowCode_CheckedChanged(object sender, EventArgs e)
        {
            if (checkShowCode.Checked)
            {
                txtItemCode.PasswordChar = '\0';
                
            }
            else
            {
                txtItemCode.PasswordChar = '*';
            }
        }
    }
}